<?php
/**
 * created by zzy
 * date: 2017/11/21 10:39
 */

namespace App\Http\Admin\System\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Eloquent\Ygt\AdminMenu;
use App\Eloquent\Ygt\AdminRoleMenu;

class MenuController extends Controller
{
    public function index(){}
    public function lists(){
        if (request()->isMethod('post')) {

            $name           = request( 'name', '' );
            $where          = [];
            if( $name !== '' ){
                $where['name']      = ['like','%'.$name.'%'];
            }
            $lists          = AdminMenu::getList( $where );
            return $lists->toJson();
        }
        return $this->view('lists' );
    }
    public function edit(){

        $params             = $this->requestJson();
        $id                 = isset($params['id']) ? $params['id'] : 0;
        $pid                = isset($params['pid']) ? $params['pid'] : 0;
        $url                = $params['url'];
        $name               = $params['name'];
        $english_name       = $params['english_name'];

        $data               = ['pid'=>$pid,'name'=>$name,'english_name'=>$english_name];
        if( $url ){
            $data['url']    = $url;
        }
        if( $id ){
            $where          = ['id'=>$id];
            $result         = AdminMenu::updateOneData( $where, $data );
        } else {
            $result         = AdminMenu::insertOneData( $data );
        }
        if( $result ){
            return $this->ajaxSuccess( '成功' );
        } else {
            return $this->ajaxFail( '失败' );
        }
    }
    public function delete(){

        $id                 = request('id',0);
        if( $id ){
            $id_arr         = explode(',',$id);
            $where          = ['id'=>['in',$id_arr]];
            $del_result     = AdminMenu::del($where);
            if( $del_result !== false ){
                $where      = ['admin_menu_id'=>['in',$id_arr]];
                $del_result = AdminRoleMenu::del($where);
                if( $del_result !== false ){
                    return $this->ajaxSuccess('成功');
                } else {
                    return $this->ajaxFail('失败3');
                }
            } else {
                return $this->ajaxFail('失败2');
            }
        } else {
            return $this->ajaxFail('失败3');
        }
    }
}
